<?php
// +----------------------------------------------------------------------
// | 工程管理
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\ProductInfoModel;
use cmf\controller\AdminBaseController;
use think\Db;
use app\admin\model\AdminMenuModel;

class RockController extends AdminBaseController
{

    public function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        $this->assign("results", '');
        $this->assign("page", '');
        return $this->fetch();
    }

    public function index1()
    {
        $this->assign("results", '');
        $this->assign("page", '');
        return $this->fetch("index");
    }

    public function add()
    {
        $this->assign("results", '');
        $this->assign("page", '');
        return $this->fetch();
    }

    /**
     * 列表查询条件
     */
    public function getWhere()
    {
        // 查询条件
        $where = array();
        $pro_type = $this->request->param("pro_type");
        $regulation = $this->request->param("regulation");
        $stele_type = $this->request->param("stele_type");
        $length = $this->request->param("length");

        $pro_type != '' && $where[] = array('s.pro_type','eq', $pro_type);
        $regulation != '' && $where[] = array('s.regulation','eq', $regulation);
        $stele_type != '' && $where[] = array('s.stele_type','eq', $stele_type);
        $length != '' && $where[] = ['s.pro_length|s.pro_width','like', "%".$length."%"];

        $this->assign("pro_type", $pro_type);
        $this->assign("regulation", $regulation);
        $this->assign("stele_type", $stele_type);
        $this->assign("length", $length);

        return $where;
    }

    // 石材产品库列表
    public function product()
    {
        $pro_name = $this->request->param("pro_name");
        $length = $this->request->param("length");
        $where = $this->getWhere();

        $result = Db::name("product")->alias('s')
            ->where($where)
            ->where('s.pro_name', 'like', '%' . $pro_name . '%')
            ->paginate(15);

        $page = $result->render();
        $this->assign('results', $result);
        $this->assign("page", $page);
        $this->assign("pro_name", $pro_name);
        $this->assign("length", $length);
        // 类型
        $pro_type = array(
            array(
                'id' => 1,
                'name' => '商品墓'
            ),
            array(
                'id' => 2,
                'name' => '艺术墓（定制）'
            ),
            array(
                'id' => 3,
                'name' => '生态墓'
            ),
            array(
                'id' => 4,
                'name' => '节地墓'
            )
        );
        $this->assign("typeArr", $pro_type);
        // 碑型
        $bei_type = array(
            array(
                'id' => 1,
                'name' => '立碑'
            ),
            array(
                'id' => 2,
                'name' => '卧碑'
            ),
            array(
                'id' => 3,
                'name' => '纪念墙'
            ),
            array(
                'id' => 4,
                'name' => '纪念铭牌'
            ),
            array(
                'id' => 5,
                'name' => '格位'
            )
        );
        $this->assign("beiArr", $bei_type);
        // 墓穴规制
        $regulation = array(
            array(
                'id' => 1,
                'name' => '单穴'
            ),
            array(
                'id' => 2,
                'name' => '双穴'
            ),
            array(
                'id' => 3,
                'name' => '三穴'
            )
        );
        $this->assign("regulationArr", $regulation);

        return $this->fetch();
    }

    /**
     * 下拉数据
     */
    public function getALlOption()
    {
        $cate_type = Db::name("stuff")->alias('s')
            ->field("s.id,concat(c.cate_name,'-',s.stuff_name) as name,s.stuff_money,s.stuff_unit")
            ->join("dj_cate c", "c.id = s.cate_id", "left")
            ->where("s.is_delete",0)
            ->where("s.status",1)
            ->where("c.if_show",1)
            ->where("c.is_delete",0)
            ->order("list_order ASC")
            ->select();
        $this->assign("cate_type", $cate_type);
        $this->assign("cateJson", json_encode($cate_type));
        // 所有产品库
        $allData = Db::name("product")->field("id,pro_name as name")->select();
        $this->assign("allData", $allData);
        // 附件
        $annex = Db::name("annex")->field("id,annex_name as name")->select();
        $this->assign("annex", $annex);
        $this->assign("annexJson", json_encode($annex));
    }
    // 产品部件 和 附件
    public function get_product_info($id){
        $result = Db::name("product")->where([ 'id' => $id])->find();
        $this->assign($result);
        // 详情  details-basics-cover-railing
        $infoData = ProductInfoModel::where(['pro_id' => $id,'itype' => 1])->select();
        $part_arr['details'] = $infoData;
        // 基础
        $baseData = ProductInfoModel::where(['pro_id' => $id,'itype' => 2])->select();
        $part_arr['basics'] = $baseData;
        // 盖板
        $plateData = ProductInfoModel::where(['pro_id' => $id,'itype' => 3])->select();
        $part_arr['cover'] = $plateData;
        // 栏杆
        $railData = ProductInfoModel::where(['pro_id' => $id,'itype' => 4])->select();
        $part_arr['railing'] = $railData;
        // 附件
        $annexData = Db::name("product_annex")->where(['pro_id' => $id])->select();
        $part_arr['annex'] = $annexData;
//        exit(json_encode($annexData))
        return $part_arr;
    }

    // 获取产品编号
    public function selectProCode($type)
    {
        $code = Db::name("product")->field("pro_code")->where(array("pro_type" => $type))->order(array("id" => "DESC"))->find();
        if (empty($code)) {
            $recode = "0001";
        } else {
            $retcode = mb_substr($code["pro_code"], 3, 7, 'utf-8') + 1;
            if ($retcode < 10) {
                $recode = "000" . $retcode;
            } else if ($retcode > 9 && $retcode < 100) {
                $recode = "00" . $retcode;
            } else if ($retcode > 99 && $retcode < 1000) {
                $recode = "0" . $retcode;
            } else {
                $recode = $retcode;
            }
        }
        return $recode;
    }

    // 石材产品库新增
    public function producttypeadd()
    {
        return $this->fetch();
    }

    // 新增商品墓产品
    public function productsp()
    {
        if ($this->request->isPost()){
            $data = $this->request->param();
            $mb_info  = $data['mb'];
            $details  = $data['details'];//详情
            $cover  = $data['cover'];//盖板
            $railing  = isset($data['railing'])?$data['railing']:[];//栏杆
            $other  = isset($data['annex'])?$data['annex']:[];//其他
            $infoData = array_merge($details,$cover,$railing);
            if (empty($mb_info['pro_photo'])){
                return $this->error('请上传产品图片');
            }
            $type = 1;
            $code = $this->selectProCode($type);
            $code = "SPM" . $code;
            $mb_info['pro_code']    = $code;
            $mb_info['pro_type']    = $type;
            $mb_info['add_time'] = time();
            $mb_info['add_user'] = cmf_get_current_admin_id();
            $pro_id = Db::name("product")->insertGetId($mb_info);
            foreach ($infoData as $key=>&$value){
                if ($value['inumber']==0){
                    unset($infoData[$key]);
                }else{
                    if (empty($value['icate'])){
                        Db::name("product")->where(['id'=>$pro_id])->delete();
                        return $this->error($value['iname'].'：请选择材料类型！');
                    }
                    $value['add_time'] = time();
                    $value['add_user'] = cmf_get_current_admin_id();
                    $value['pro_id'] = $pro_id;
                }
            }
            Db::name("product_info")->insertAll($infoData);
            // 保存附件
            if ($mb_info['pro_annex'] == 1) {
                foreach ($other as $key => &$value) {
                    $value['add_time'] = time();
                    $value['add_user'] = cmf_get_current_admin_id();
                    $value['pro_id'] = $pro_id;
                }
                Db::name("product_annex")->insertAll($other);
            }
            $this->success("保存成功", url("Rock/product"));
        }
        // 石材种类
        $this->getALlOption();
        $part_arr = $this->part_arr();
        $this->assign('partJson', json_encode($part_arr[1]));
        return $this->fetch();
    }

    // 商品墓编辑
    public function productspedit()
    {
        if ($this->request->isPost()){
            $data = $this->request->param();
            $pro_id = $this->request->param('id');
            $mb_info  = $data['mb'];
            $details  = $data['details'];//详情
            $basics  = $data['basics'];// 基石 和 贴面
            $cover  = $data['cover'];//盖板
            $railing  = isset($data['railing'])?$data['railing']:[];//栏杆
            $other  = isset($data['annex'])?$data['annex']:[];//其他
            $array = array_merge($details,$basics,$cover,$railing);

            if (empty($mb_info['pro_photo'])){
                return $this->error('请上传产品图片');
            }
//            $pro_id = $mb_info['id'];
            unset($mb_info['id']);
            Db::name("product")->where('id', $pro_id)->update($mb_info);

            foreach ($array as $key=>&$value){
                if ($value['inumber']==0){
//                    unset($array[$key]);
                    // 删除product_info表的这条数据
                    Db::name("product_info")->where(['id'=>$value['id'],'pro_id'=>$pro_id])->delete();
                }else {
                    $id = $value['id'];
                    unset($value['id']);
                    Db::name("product_info")->where('id', $id)->update($value);
                }
            }
            // 保存附件
            foreach ($other as $key=>&$value){
                $oid=$value['id'];
                unset($value['id']);
                Db::name("product_annex")->where(['id'=>$oid])->update($value);
            }
           return  $this->success("保存成功", url("Rock/product"));
        }

        // 石材种类
        $this->getALlOption();
        $id = $this->request->param("id");
        $part_arr = $this->get_product_info($id);
        $this->assign('partJson', json_encode($part_arr));
        return $this->fetch();
    }





    // 新增艺术墓产品
    public function productys()
    {
        // 石材种类
        $this->getALlOption();
        return $this->fetch();
    }

    // 新增生态墓产品
    public function productst()
    {
        // 石材种类
        $this->getALlOption();
        return $this->fetch();
    }

    // 生态墓新增保存
    public function productstadd_post()
    {
        $data = $this->request->param();
        $type = $data['pro_type'];
        $code = $this->selectProCode($type);
        $code = "STM" . $code;
        $productData['pro_code'] = $code;
        $productData['pro_type'] = $type;
        $productData['pro_name'] = $data['pro_name'];
        $productData['pro_status'] = $data['pro_status'];
        $productData['regulation'] = $data['regulation'];
        $productData['stele_type'] = $data['stele_type'];
        $productData['pro_length'] = $data['pro_length'];
        $productData['pro_width'] = $data['pro_width'];
        $productData['pro_photo'] = $data['pro_photo'];
        $productData['pro_price'] = $data['pro_price'];
        $productData['pro_height'] = "";
        $productData['add_time'] = time();
        $productData['add_user'] = cmf_get_current_admin_id();

        $pro_id = Db::name("product")->insertGetId($productData);

        // 保存详情表

        for ($i = 0; $i < count($data['iname']); $i++) {
            $infoData[$i]['itype'] = 1;
            $infoData[$i]['iname'] = $data['iname'][$i];
            $infoData[$i]['iwidth'] = $data['iwidth'][$i];
            $infoData[$i]['iheight'] = $data['iheight'][$i];
            $infoData[$i]['ithick'] = $data['ithick'][$i];
            $infoData[$i]['icate'] = $data['icate'][$i];
            $infoData[$i]['inumber'] = $data['inumber'][$i];
            $infoData[$i]['imaterial_price'] = $data['imaterial_price'][$i];
            $infoData[$i]['irepair_price'] = $data['irepair_price'][$i];
            $infoData[$i]['ichange_price'] = $data['ichange_price'][$i];
            $infoData[$i]['isort'] = $data['isort'][$i];
            $infoData[$i]['add_time'] = time();
            $infoData[$i]['add_user'] = cmf_get_current_admin_id();
            $infoData[$i]['pro_id'] = $pro_id;
        }
        Db::name("product_info")->insertAll($infoData);

        $this->success("保存成功", url("Rock/product"));
    }

    // 生态墓编辑
    public function productstedit()
    {
        if ($this->request->isPost()){
            $data = $this->request->param();
            $mb_info  = $data['mb'];
            $details  = $data['details'];//详情
            $pro_id = $mb_info['id'];
            unset($mb_info['id']);
            Db::name("product")->where('id', $pro_id)->update($mb_info);

            foreach ($details as $key=>&$value){
                $id = $value['id'];
                unset($value['id']);
                Db::name("product_info")->where('id', $id)->update($value);
            }
            $this->success("保存成功", url("Rock/product"));
        }
        // 石材种类
        $this->getALlOption();

        $id = $this->request->param("id");
        $where = array(
            'id' => $id
        );

        $result = Db::name("product")->where($where)->find();
        $this->assign($result);
        // 详情
        $infowhere = array(
            'pro_id' => $id,
            'itype' => 1
        );
        $infoData = Db::name("product_info")->where($infowhere)->select();
        $this->assign('infoData', $infoData);
        return $this->fetch();
    }

    // 新增节地墓产品
    public function productjd()
    {
        // 石材种类
        $this->getALlOption();
        return $this->fetch();
    }

    // 节地墓新增保存
    public function productjdadd_post()
    {
        $data = $this->request->param();
        $type = $data['pro_type'];
        $code = $this->selectProCode($type);
        $code = "JDM" . $code;
        $productData['pro_code'] = $code;
        $productData['pro_type'] = $type;
        $productData['pro_name'] = $data['pro_name'];
        $productData['pro_status'] = $data['pro_status'];
        $productData['regulation'] = $data['regulation'];
        $productData['stele_type'] = $data['stele_type'];
        $productData['pro_length'] = $data['pro_length'];
        $productData['pro_width'] = $data['pro_width'];
        $productData['pro_height'] = $data['pro_height'];
        $productData['pro_annex'] = $data['pro_annex'];
        $productData['pro_photo'] = $data['pro_photo'];
        $productData['pro_price'] = $data['pro_price'];
        $productData['add_time'] = time();
        $productData['add_user'] = cmf_get_current_admin_id();

        $pro_id = Db::name("product")->insertGetId($productData);

        // 保存详情表
        for ($i = 0; $i < count($data['iname']); $i++) {
            $infoData[$i]['itype'] = 1;
            $infoData[$i]['iname'] = $data['iname'][$i];
            $infoData[$i]['iwidth'] = $data['iwidth'][$i];
            $infoData[$i]['iheight'] = $data['iheight'][$i];
            $infoData[$i]['ithick'] = $data['ithick'][$i];
            $infoData[$i]['icate'] = $data['icate'][$i];
            $infoData[$i]['inumber'] = $data['inumber'][$i];
            $infoData[$i]['imaterial_price'] = $data['imaterial_price'][$i];
            $infoData[$i]['irepair_price'] = $data['irepair_price'][$i];
            $infoData[$i]['ichange_price'] = $data['ichange_price'][$i];
            $infoData[$i]['isort'] = $data['isort'][$i];
            $infoData[$i]['add_time'] = time();
            $infoData[$i]['add_user'] = cmf_get_current_admin_id();
            $infoData[$i]['pro_id'] = $pro_id;

            if ($data['inumber'][$i]==0){
                unset($infoData[$i]);
            }else{
                if (empty($infoData[$i]['icate'])){
                    Db::name("product")->where(['id'=>$pro_id])->delete();
                    return $this->error($infoData[$i]['iname'].'：请选择材料类型！');
                }
                $infoData[$i]['add_time'] = time();
                $infoData[$i]['add_user'] = cmf_get_current_admin_id();
                $infoData[$i]['pro_id'] = $pro_id;
            }

        }
        Db::name("product_info")->insertAll($infoData);
        // 保存附件
        if ($data['pro_annex'] == 1) {
            for ($i = 0; $i < count($data['annex_id']); $i++) {
                $annexData[$i]['annex_id'] = $data['annex_id'][$i];
                $annexData[$i]['anumber'] = $data['anumber'][$i];
                $annexData[$i]['add_time'] = time();
                $annexData[$i]['add_user'] = cmf_get_current_admin_id();
                $annexData[$i]['pro_id'] = $pro_id;
            }
            Db::name("product_annex")->insertAll($annexData);
        }
        $this->success("保存成功", url("Rock/product"));
    }

    // 节地墓编辑
    public function productjdedit()
    {
        if ($this->request->isPost()){
            $data = $this->request->param();
            $mb_info  = $data['mb'];
            $details  = $data['details'];//详情
            $other  = isset($data['annex'])?$data['annex']:[];//其他
            $pro_id = $mb_info['id'];
            unset($mb_info['id']);
            Db::name("product")->where('id', $pro_id)->update($mb_info);

            foreach ($details as $key=>&$value){

                if ($value['inumber']=="0"){
                    // 删除product_info表的这条数据
                    Db::name("product_info")->where(['id'=>$value['id'],'pro_id'=>$pro_id])->delete();
                }else {
                    $id = $value['id'];
                    unset($value['id']);
                    Db::name("product_info")->where('id', $id)->update($value);
                }
            }
            // 保存附件
            foreach ($other as $key=>&$value){
                $oid=$value['id'];
                unset($value['id']);
                Db::name("product_annex")->where(['id'=>$oid])->update($value);
            }

            $this->success("保存成功", url("Rock/product"));
        }

        // 石材种类
        $this->getALlOption();

        $id = $this->request->param("id");
        $where = array(
            'id' => $id
        );

        $result = Db::name("product")->where($where)->find();
        $this->assign($result);
        // 详情
        $infowhere = array(
            'pro_id' => $id,
            'itype' => 1
        );
        $infoData = Db::name("product_info")->where($infowhere)->select();
        $this->assign('infoData', $infoData);

        // 附件
        $annexwhere = array(
            'pro_id' => $id,
        );
        $annexData = Db::name("product_annex")->where($annexwhere)->select();
        $this->assign('annexData', $annexData);
        return $this->fetch();
    }

    // 节地墓编辑保存
    public function productjdedit_post()
    {
        $pro_id = $this->request->param('id');
        $data = $this->request->param();
        $productData['pro_code'] = $data['pro_code'];
        $productData['pro_type'] = $data['pro_type'];
        $productData['pro_name'] = $data['pro_name'];
        $productData['pro_status'] = $data['pro_status'];
        $productData['regulation'] = $data['regulation'];
        $productData['stele_type'] = $data['stele_type'];
        $productData['pro_length'] = $data['pro_length'];
        $productData['pro_width'] = $data['pro_width'];
        $productData['pro_height'] = $data['pro_height'];
        $productData['pro_annex'] = $data['pro_annex'];
        $productData['pro_photo'] = $data['pro_photo'];
        $productData['pro_price'] = $data['pro_price'];

        $productData['add_time'] = time();

        Db::name("product")->where('id', $pro_id)->update($productData);
        Db::name("product_annex")->where('pro_id', $pro_id)->delete();
        // 删除详情表
        Db::name("product_info")->where('pro_id', $pro_id)->delete();
        // 保存详情表
        for ($i = 0; $i < count($data['iname']); $i++) {
            $infoData[$i]['itype'] = 1;
            $infoData[$i]['iname'] = $data['iname'][$i];
            $infoData[$i]['iwidth'] = $data['iwidth'][$i];
            $infoData[$i]['iheight'] = $data['iheight'][$i];
            $infoData[$i]['ithick'] = $data['ithick'][$i];
            $infoData[$i]['icate'] = $data['icate'][$i];
            $infoData[$i]['inumber'] = $data['inumber'][$i];
            $infoData[$i]['imaterial_price'] = $data['imaterial_price'][$i];
            $infoData[$i]['irepair_price'] = $data['irepair_price'][$i];
            $infoData[$i]['ichange_price'] = $data['ichange_price'][$i];
            $infoData[$i]['isort'] = $data['isort'][$i];
            $infoData[$i]['add_time'] = time();
            $infoData[$i]['add_user'] = cmf_get_current_admin_id();
            $infoData[$i]['pro_id'] = $pro_id;
        }
        Db::name("product_info")->insertAll($infoData);
        // 保存附件
        if ($data['pro_annex'] == 1) {
            for ($i = 0; $i < count($data['annex_id']); $i++) {
                $annexData[$i]['annex_id'] = $data['annex_id'][$i];
                $annexData[$i]['anumber'] = $data['anumber'][$i];
                $annexData[$i]['add_time'] = time();
                $annexData[$i]['add_user'] = cmf_get_current_admin_id();
                $annexData[$i]['pro_id'] = $pro_id;
            }
            Db::name("product_annex")->insertAll($annexData);
        }
        $this->success("保存成功", url("Rock/product"));
    }

    // 更改状态
    public function producttoggle()
    {
        $id = $this->request->param("id");
        $pro_status = $this->request->param("pro_status");
        $saveData = array(
            'id' => $id,
            'pro_status' => $pro_status == '1' ? '2' : '1'
        );
        if ($pro_id = Db::name("product")->update($saveData) !== false) {
            $this->success("操作成功！");
        } else {
            $this->error("操作失败！");
        }
    }

    // 石材产品库删除
    public function productdel()
    {
        $id = $this->request->param("id");
        Db::name("product_annex")->where('pro_id', $id)->delete();
        Db::name("product_info")->where('pro_id', $id)->delete();
        Db::name("product")->where('id', $id)->delete();
        $this->success("删除成功", url('Rock/product'));
    }

    // 新增艺术墓墓产品
    public function productysmb()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();

            $mb_info  = $data['mb'];
            $details  = $data['details'];//详情
            $basics  = $data['basics'];// 基石 和 贴面 
            $cover  = isset($data['cover'])?$data['cover']:[];//盖板
            $railing  = isset($data['railing'])?$data['railing']:[];//栏杆
            $other  = isset($data['annex'])?$data['annex']:[];//其他
            $infoData = array_merge($details,$basics,$cover,$railing);

            if (empty($mb_info['pro_name'])){
                return $this->error('请填写产品名称！');
            }
            /* if (empty($mb_info['pro_length'])){
                return $this->error('请填写占地长度！');
            }
            if (empty($mb_info['pro_width'])){
                return $this->error('请填写占地宽度！');
            }
            if (empty($mb_info['pro_height'])){
                return $this->error('请填写墓碑总高！');
            }*/
            if (empty($mb_info['pro_price'])){
                return $this->error('请填写产品价格！');
            }
            if (empty($mb_info['pro_photo'])){
                return $this->error('请上传产品图片');
            }

            $type = 2;
            $code = $this->selectProCode($type);
            $code = "SPM" . $code;
            $mb_info['pro_code']    = $code;
            $mb_info['pro_type'] = $type;
            $mb_info['add_time'] = time();
            $mb_info['add_user'] = cmf_get_current_admin_id();
            $pro_id = Db::name("product_mb")->insertGetId($mb_info);
            $infoData_arr = [];
            foreach ($infoData as $key=>&$value){
                if ($value['inumber']==0){
                    unset($infoData[$key]);
                }else{
                    if (empty($value['icate'])){
                        Db::name("product_mb")->where(['id'=>$pro_id])->delete();
                        return $this->error($value['iname'].'：请选择材料类型！');
                    }
                    $value['add_time'] = time();
                    $value['add_user'] = cmf_get_current_admin_id();
                    $value['pro_id'] = $pro_id;
                    $infoData_arr[]= $value;
                }
            }
            Db::name("product_mb_info")->insertAll($infoData);
            // 保存附件
            $other_arr =[];
            if ($mb_info['pro_annex'] == 1) {
                foreach ($other as $key => &$value) {
                    if(empty($value['annex_id'])){
                        return $this->error('请选择附件！');
                    }
                    $value['add_time'] = time();
                    $value['add_user'] = cmf_get_current_admin_id();
                    $value['mb_id'] = $pro_id;
                    $other_arr[] = $value;
                }
                Db::name("product_mb_annex")->insertAll($other);
            }
            return $this->success('添加成功','',['pro_id' => $pro_id, 'pro_name' => $mb_info['pro_name'],'regulation'=>$mb_info['regulation']]);
        }

        $part_arr = $this->part_arr();
        $this->assign('partJson', json_encode($part_arr[1]));
        $this->assign('type_mb', $this->request->param('type'));
        // 石材种类
        $this->getALlOption();
        return $this->fetch();
    }
    //艺术墓详细信息编辑
    public function productysmbedit()
    {
        if ($this->request->isPost()){
            $data = $this->request->param();
            $mb_info  = $data['mb'];
            $details  = $data['details'];//详情
            $basics  = $data['basics'];// 基石 和 贴面
            $cover  = isset($data['cover'])?$data['cover']:[];//盖板
            $railing  = isset($data['railing'])?$data['railing']:[];//栏杆
            $other  = isset($data['annex'])?$data['annex']:[];//其他
            $array = array_merge($details,$basics,$cover,$railing);

            $pro_id  = $mb_info['id'];
            unset($mb_info['id']);
            $mb_info['add_time'] = time();
            Db::name("product_mb")->where('id', $pro_id)->update($mb_info);
            // 保存基础表
            foreach ($array as $key=>&$val){
                $did = $val['id'];
                unset($val['id']);
                Db::name("product_mb_info")->where('id', $did)->update($val);
            }
            // 保存附件
            foreach ($other as $k=>&$v){
                $id = $v['id'];
                unset($val['id']);
                Db::name("product_mb_annex")->where('id', $id)->update($v);
            }
            return $this->success('添加成功','',['pro_id' => $pro_id, 'pro_name' => $mb_info['pro_name'],'regulation'=>$mb_info['regulation']]);
        }
        // 石材种类
        $this->getALlOption();

        $id = $this->request->param("id");
        $type = $this->request->param('type');
        $result = Db::name("product_mb")->where(['id' => $id])->find();


        $this->assign($result);
        // 详情  details-basics-cover-railing
        $infoData =  Db::name("product_mb_info")->where(['pro_id' => $id,'itype' => 1])->select();
        $part_arr['details'] = $infoData;
        // 基础
        $baseData =  Db::name("product_mb_info")->where(['pro_id' => $id,'itype' => 2])->select();
        $part_arr['basics'] = $baseData;
        // 盖板
        $plateData =  Db::name("product_mb_info")->where(['pro_id' => $id,'itype' => 3])->select();
        $part_arr['cover'] = $plateData;
        // 栏杆
        $railData =  Db::name("product_mb_info")->where(['pro_id' => $id,'itype' => 4])->select();
        $part_arr['railing'] = $railData;
        // 附件
        $annexData = Db::name("product_mb_annex")->where(['mb_id' => $id])->select();
        $part_arr['annex'] = $annexData;

        $this->assign('partJson', json_encode($part_arr));
        $this->assign('type_mb', $type);
        return $this->fetch();
    }


    protected function part_arr(){
//        $details  = $data['details'];//详情
//        $basics  = $data['basics'];// 基石 和 贴面
//        $cover  = $data['cover'];//盖板
//        $railing  = isset($data['railing'])?$data['railing']:[];//栏杆

        $array[1]['details'] = [
            ['itype'=>1,'iname'=>'碑笠','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>1],
            ['itype'=>1,'iname'=>'碑傍','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>2],
            ['itype'=>1,'iname'=>'主碑板','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>3],
            ['itype'=>1,'iname'=>'副碑板','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>4],
            ['itype'=>1,'iname'=>'碑座','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>5],
            ['itype'=>1,'iname'=>'前探柱','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>6],
        ];
        $array[1]['basics'] = [
                'zs'=>[
                    ['itype'=>2,'iname'=>'一级底座前芝台','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>1],
                    ['itype'=>2,'iname'=>'一级底座侧芝台','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>2],
                    ['itype'=>2,'iname'=>'一级底座后芝台','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>3],
                    ['itype'=>2,'iname'=>'二级底座前芝台','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>4],
                    ['itype'=>2,'iname'=>'二级底座侧芝台','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>5],
                    ['itype'=>2,'iname'=>'二级底座后芝台','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>6],
                ],
                    'tm'=>[
                ['itype'=>2,'iname'=>'一级底座前基石','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>1],
                ['itype'=>2,'iname'=>'一级底座后基石','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>2],
                ['itype'=>2,'iname'=>'一级底座侧基石','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>3],
                ['itype'=>2,'iname'=>'一级踏步前基石','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>4],
                ['itype'=>2,'iname'=>'一级踏步侧基石','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>6],
                ['itype'=>2,'iname'=>'一级踏步面板','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>7],
                ['itype'=>2,'iname'=>'二级踏步前基石','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>8],
                ['itype'=>2,'iname'=>'二级踏步侧基石','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>9],
                ['itype'=>2,'iname'=>'二级踏步面板','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>10],
                ['itype'=>2,'iname'=>'底座基石上面板','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>10],

            ]
        ];
        $array[1]['cover']  = [
            ['itype'=>3,'iname'=>'陛阶石','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>11],
            ['itype'=>3,'iname'=>'盖板（可刻字）','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>1],
            ['itype'=>3,'iname'=>'盖板1','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>2],

        ];
        $array[1]['railing'] = [
            ['itype'=>4,'iname'=>'后栏杆柱','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>1],
            ['itype'=>4,'iname'=>'前栏杆柱','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>2],
            ['itype'=>4,'iname'=>'栏板/羽目','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>3],
            ['itype'=>4,'iname'=>'抱古','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>4],
            ['itype'=>4,'iname'=>'圆雕狮子','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>5],
        ];

        $array[4]['jiedimu'] =  [
            ['itype'=>4,'iname'=>'上下隔板','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>1],
            ['itype'=>4,'iname'=>'左右隔板','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>2],
            ['itype'=>4,'iname'=>'后隔板','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>3],
            ['itype'=>4,'iname'=>'装饰门','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>4],
            ['itype'=>4,'iname'=>'上下内装饰面','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>5],
            ['itype'=>4,'iname'=>'左右内装饰面','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>6],
            ['itype'=>4,'iname'=>'前后内装饰面','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>7],
            ['itype'=>4,'iname'=>'艺术造型','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>8],
            ['itype'=>4,'iname'=>'','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>9],
            ['itype'=>4,'iname'=>'','iwidth'=>1,'iheight'=>1,'iheight','ithick'=>1,'icate'=>1,'inumber'=>1,'imaterial_price'=>1,'irepair_price'=>1,'ichange_price'=>1,'isort'=>10],
        ];


        return $array;
    }



}
